package com.hry.store.service.impl;

import com.hry.store.dao.UserMapper;
import com.hry.store.pojo.User;
import com.hry.store.service.MailService;
import com.hry.store.service.UserService;
import com.hry.store.service.VerifyCodeService;
import com.hry.store.util.MD5Util;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MailService mailService;
    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     * 用户注册
     *
     * @param username
     * @param password
     * @param phone
     * @param email
     */
    @Override
    public void register(String username, String password, String phone, String email) {
        User user = new User();
        user.setUserName(username);
        user.setPhone(phone);
        user.setPassword(MD5Util.MD5EncodeUtf8(password));
        user.setUserEmail(email);
        // 设置成未激活状态
        user.setStatus(0);
        // 调用生成邮箱校验码的方法
        String key = generateVerifyEmailCode();
        user.setVerifyCode(key);
        mailService.registerNotify(email, key);
        userMapper.insertSelective(user);
    }

    /**
     * 检查邮箱是否存在
     *
     * @param checkEmail
     * @return
     */
    @Override
    public Boolean checkEmail(String checkEmail) {
        int resultCount = userMapper.checkEmail(checkEmail);
        if (resultCount > 0) {
            return false;
        }
        return true;
    }

    /**
     * 激活用户
     * @param key
     * @return
     */
    @Override
    public boolean activate(String email, String key) {
        int resultCount = userMapper.selectKeyByEmail(email, key);
        if (resultCount > 0) {
            return mailService.enable(email, key);
        }
        return false;
    }

    /**
     * 用户登录
     * @param email
     * @param password
     * @param verifyCode
     */
    @Override
    public boolean login(String email, String password, int verifyCode, HttpServletRequest request) throws Exception {
        Boolean result = verifyCodeService.checkVerifyEmail(verifyCode, request);
        if (result) {
            // 有该用户，并且已经被激活
            int resultCount = userMapper.login(email, MD5Util.MD5EncodeUtf8(password));
            if (resultCount > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据用户邮件查询出用户基本信息
     * @param email
     * @return
     */
    @Override
    public User getUserInfo(String email) {
        return userMapper.getUserInfoByEmail(email);
    }

    /**
     * 修改用户信息
     * @param name
     * @param email
     * @param phone
     * @param sign
     * @param favorite
     * @return
     */
    @Override
    public int updateUser(Integer userId,String name, String email, Long phone, String sign, String favorite) {
        User user = new User();
        user.setUserId(userId);
        user.setUserName(name);
        user.setUserEmail(email);
        user.setPhone(String.valueOf(phone));
        user.setPersonSign(sign);
        user.setFavorite(favorite);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 生成邮箱验证码
     *
     * @return
     */
    private String generateVerifyEmailCode() {
        return RandomStringUtils.randomAlphabetic(10);
    }
}
